package ioperations;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:张熳
 * Date:2025-09-04
 * Time:17:16
 */
//增加

/**
 * 1.输入各种成员变量 构建对象
 * 2.判断是否满
 * 3.放到数组的最后一个位置
 * 4.usedSize++
 */

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书📖");
        //判满
        int currentSize = bookList.getUsedSize();
        if (currentSize == bookList.getBooks().length) {
            System.out.println("书架满了！");
            return;
        }
        //构建对象
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入新增图书书名：");
        String name = scanner.nextLine();
        System.out.print("请输入新增图书作者：");
        String author = scanner.nextLine();
        System.out.print("请输入书的类型：");
        String type = scanner.nextLine();
        System.out.print("请输入新增图书价格：");//这里注意字符串写在整型前面，否则要处理\n
        int price = scanner.nextInt();

        Book newbook = new Book(name,author,price,type);
        //判断书架有没有这本要增加的书
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                System.out.println("书架中已有这本书！");
                return;
            }
        }
        //插入这本书
        bookList.setBook(currentSize,newbook);
        bookList.setUsedSize(currentSize+1);
        System.out.println("新增图书成功！");
    }
}
